#!/usr/bin/perl

use cm2bug;
use Getopt::Long;
use File::Basename;

$_PRG=basename($0);

%records=(
	"users"	=> [
			["bugadmin","Bug Database Administrator","simon.edwards\@bankofamerica.com","81075de2922da188cce0f4b4ab70d26237e3cfd8" ],
			["anonymous","Anonymous bug reporter","","f7b826e3ec9e8296bb2de32783cce74242c6a32d"],
		],
	"bugtypes" => [
			["Bug","Standard bug",10],
			["Enhancement","Improved functionality",20],
			["Change","Modify existing functionality",30],
			["Request","Possible enhancement",40],
		],
);

%tables=(
	"users"		=>
		"CREATE TABLE users(
			username VARCHAR(50) PRIMARY KEY,
			desc VARCHAR(200),
			mailid VARCHAR(100),
			password VARCHAR(50)
		);",
	"groups"	=> 
		"CREATE TABLE groups(
			groupname VARCHAR(50) PRIMARY KEY,
			desc VARCHAR(200),
			users VARCHAR(200)
		);",
	"projects"	=> 
		"CREATE TABLE projects(
			projectname VARCHAR(50) PRIMARY KEY,
			desc VARCHAR(200),
			patch_emails CHAR(1),
			patch_emails_to VARCHAR(100)
		);",
	"bugtypes"	=>
		"CREATE TABLE bugtypes(
			type VARCHAR(50) PRIMARY KEY,
			desc VARCHAR(200),
			priority INTEGER
		);",
	"bugs"		=>
		"CREATE TABLE bugs(
			bug INTEGER PRIMARY KEY,
			type VARCHAR(50),
			raised_date DATE,
			raised_by VARCHAR(50),
			inform_users VARCHAR(200),
			inform_groups VARCHAR(200),
			desc VARCHAR(1024),
			projects_affected VARCHAR(200),
			assigned_to_users VARCHAR(200),
			assigned_to_groups VARCHAR(200),
			children VARCHAR(200),
			state VARCHAR(30),
			time_est FLOAT
		);",
	"buglogs"	=>
		"CREATE TABLE buglogs(
			number INTEGER PRIMARY KEY,
			bug INTEGER,
			submitter VARCHAR(50),
			type VARCHAR(50),
			text VARCHAR(1024),
			time FLOAT,
			new_state VARCHAR(50),
			patch_id VARCHAR(50)
		);",
	"audit"		=>
		"CREATE TABLE audit(
			number INTEGER PRIMARY KEY,
			user VARCHAR(50),
			action VARCHAR(50),
			object VARCHAR(50),
			result INTEGER,
			text VARCHAR(200)
		);",
);


sub usage {
	print "
Usage: $_PRG [--dbfile S]

The $_PRG utility is used to create an initial database file for
the CM2 bug-tracker module. At present it supports the following arguments:

--dbfile	The name of the file to create - which must not exist.

Currently it will generate the following tables:

";
	print "  * $_\n" for (sort(keys %tables));
	print "\n";
	exit 1;
}

$ARG_dbfile="dbfile";
if(!GetOptions(
	"dbfile=s"	=> \$ARG_dbfile,
)) {
	usage;
}

if(-f $ARG_dbfile) {
	print STDERR "Error: File $ARG_dbfile already exists - not overwriting.\n";
	exit 1;
}

$DBH = cm2bug->new($ARG_dbfile);
if(!defined($DBH)) {
	($rc,$str)=cm2bug->error;
	print STDERR "Error: $str [rc=$rc]\n";
	exit 1;
}

# Firstly drop all tables ...

for $table (sort(keys %tables)) {
	$DBH->do("DROP TABLE $table");
	$DBH->commit;
}

# And now create them again!

for $table (sort(keys %tables)) {
	$DBH->do($tables{$table});
	($cc,$rc)=$DBH->error;
	if(defined($rc)) {
		if($rc =~ /already exists/) {
			print "Log  : Table \"$table\" already exists.\n";
		} else {
			print STDERR "Error: Failed to create table $table - $rc:\n";
		}
	} else {
		print "Log  : Table \"$table\" created successfully.\n";
		$DBH->commit;
	}
}

#########################################################################
# Once the tables have been created add any necessary base records.	#
#########################################################################

for $rectype (sort(keys(%records))) {
	for $crec (@{$records{$rectype}}) {
		$cmd="INSERT INTO $rectype VALUES(";
		$cmd.="'$_'," foreach(@$crec);
		$cmd =~ s/,$//;
		$cmd.=");";
		$DBH->do($cmd);
		($cc,$rc)=$DBH->error;
		if(defined($rc)) {
			print "Error: Unable to insert record into $rectype:\n";
			print "Error: $rc\n";
			exit 1;
		}
		$DBH->commit;
	}
}

$DBH->disconnect;

exit;
